package com.kaikai.edu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaikai.commonutils.R;
import com.kaikai.edu.entity.EduCourse;
import com.kaikai.edu.entity.chapter.ChapterVo;
import com.kaikai.edu.entity.frontvo.CourseFrontQueryInfo;
import com.kaikai.edu.entity.frontvo.CourseFrontVo;
import com.kaikai.edu.service.EduChapterService;
import com.kaikai.edu.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vo.EduCourseOrder;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("eduservice/frontCourse")
public class EduCourseFrontController {

    @Autowired
    private EduChapterService chapterService;


    @Autowired
    private EduCourseService courseService;

    //根据条件查询课程信息
    @PostMapping("pageCourseList/{page}/{limit}")
    public R pageCourseList(@PathVariable Long page,@PathVariable Long limit,@RequestBody(required = false) CourseFrontQueryInfo queryInfo){
        Page<EduCourse> coursePage=new Page<>(page,limit);
        Map<String,Object> courseInfo=courseService.pageListCourse(coursePage,queryInfo);
        return R.ok().data(courseInfo);
    }
    //根据courseId查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseWebInfo(@PathVariable String courseId){
        //基本信息查询
        CourseFrontVo vo=courseService.getCourseWebInfo(courseId);
        //查询章节小节
        List<ChapterVo> chapterVoList = chapterService.getChapterVideo(courseId);
        return R.ok().data("CourseInfo",vo).data("chapterList",chapterVoList);
    }

    //调用所需,通过id获取课程信息

    /**
     * 讲师id,名称,课程封面,课程id,课程标题,课程价格
     * @param courseId
     * @return
     */
    @GetMapping("getDto/{courseId}")
    public EduCourseOrder getDto(@PathVariable String courseId){
        EduCourse course = courseService.getById(courseId);
        EduCourseOrder courseOrder=new EduCourseOrder();
        BeanUtils.copyProperties(course, courseOrder);
        return courseOrder;
    }
}
